from odoo import api, fields, models

DISCOUNT_ELIGIBILITY = [('all_mail_route', '所有邮路'), ('single_value_add', '单一增值'), ('single_mail_route', '单一邮路'), ('all_value_add', '所有增值(二类)')]
COUPON_TYPE = [('spend_save', '满减券'), ('Discount', '折扣券'), ('free_charge', '全免券')]
COUPON_STATUS = [('not_active', '未生效'), ('not_use', '未使用'), ('used', '已使用'), ('expired', '过期')]
VALIDITY_SELECTION = [('day', '天'), ('week', '周'), ('month', '月'), ('year', '年')]


class RpCustomerCoupon(models.Model):
    _name = 'rp.customer.coupon'
    _description = '客户优惠券'
    _inherit = ['mail.thread', 'mail.activity.mixin']
    _sql_constraints = [('code_unique', 'UNIQUE(code)', '序列号必须唯一')]

    # 基本信息
    rp_customer_id = fields.Many2one(string='客户', comodel_name='rp.customer', ondelete='cascade', required=True, copy=False)
    name = fields.Char(string='名称', copy=False, tracking=True)
    code = fields.Char(string='序列号', default='RP_COU/****', copy=False, index=True, tracking=True)
    coupon_type = fields.Selection(string='优惠券类型', selection=COUPON_TYPE, default=COUPON_TYPE[0][0], copy=False, tracking=True)
    par_value = fields.Float(string='面值', copy=False, tracking=True, store=True)
    discount_value = fields.Float(string='折扣', copy=False, tracking=True, store=True)
    coupon_threshold = fields.Float(string='门槛', copy=False, tracking=True, store=True)
    is_stacked = fields.Boolean(string='叠加', copy=False, tracking=True, store=True)
    rp_coupon_validity_selection = fields.Selection(string='生效选项', selection=VALIDITY_SELECTION, default=VALIDITY_SELECTION[0][0], required=True, copy=False, store=True, tracking=True)
    rp_coupon_validity = fields.Integer(string='时效', copy=False, tracking=True, index=True, store=True)
    currency_id = fields.Many2one(string='货币单位', comodel_name='res.currency', default=lambda self: self.env.company.currency_id)
    customer_coupon_start_date = fields.Date(string='生效时间', store=True, copy=False)
    customer_coupon_expired_date = fields.Date(string='截止时间', required=True, copy=False)
    rp_coupon_status = fields.Selection(string='状态', selection=COUPON_STATUS, default=COUPON_STATUS[1][0], required=True, copy=False)

    # 优惠关联信息
    coupon_for = fields.Selection(string='优惠对象', selection=DISCOUNT_ELIGIBILITY, copy=False, store=True)
    rp_coupon_template_id = fields.Many2one(string='优惠券模板', comodel_name='rp.marketing.coupon.template', required=True, copy=False)
    rp_coupon_mail_route_id = fields.Many2one(string='单一邮路', comodel_name='rp.shipper.mail.route', copy=False, tracking=True)
    rp_coupon_valued_added_id = fields.Many2one(string='单一增值', comodel_name='rp.shipper.sale.template.value.added.line', copy=False)

    remark = fields.Text(string='备注', related='rp_coupon_template_id.remark', store=True)

    @api.model_create_multi
    def create(self, vals_list):
        for vals in vals_list:
            vals['code'] = self.env['ir.sequence'].sudo().next_by_code('rp.customer.coupon') or '/'
        return super(RpCustomerCoupon, self).create(vals_list)

    def _get_name(self):
        """重写display_name"""
        name = self.name
        if self._context.get('show_code'):
            name = self.code if self.code else ''
        return name

    def name_get(self):
        res = []
        for record in self:
            name = record._get_name()
            res.append((record.id, name))
        return res
